<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: default.php 353 2013-10-19 10:44:12Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 353 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-10-19 10:44:12 +0000 (Sat, 19 Oct 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$default_smarty = new Smarty();
$default_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
$default_smarty->assign('session', session_id());
$main_content = '';

require_once(DIR_FS_INC.'inc.customer_greeting.php');
require_once(DIR_FS_INC.'inc.get_path.php');
require_once(DIR_FS_INC.'inc.check_categories_status.php');

if(check_categories_status($current_category_id) >= 1) {
	$error = CATEGORIE_NOT_FOUND;
	include(DIR_WS_MODULES.FILENAME_ERROR_HANDLER);

} else {
	if(GROUP_CHECK == 'true') {
		$group_check_c = "AND c.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 "; // Kategorie
		$group_check_p = "AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 "; // Produkt
	}
	
	$total_ids = '';
	$fsk_lock = '';
	
	if($_SESSION['customers_status']['customers_fsk18_display'] == '0')
		$fsk_lock = ' AND p.products_fsk18 !=1';
	
	if ($category_depth == 'nested') {

		$category = $db->db_query("SELECT
				                    	cd.categories_description,
				                    	cd.categories_name,
				          				cd.categories_heading_title,
				                        c.categories_template,
				                        c.categories_image,
				                        cd.categories_pic_alt
				                   FROM
				                   		".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd
				                   WHERE
				                   		c.categories_id = '".$current_category_id."'
				                   AND
				                   		cd.categories_id = '".$current_category_id."'
				                        ".$group_check_c."
				                   AND
				                   		cd.language_id = '".(int)$_SESSION['languages_id']."'", true);

		if(isset($cPath) && preg_match('/_/', $cPath)) {
			$category_links = array_reverse($cPath_array);
			for ($i = 0, $n = sizeof($category_links); $i < $n; $i ++) {

				$categories = $db->db_query("SELECT
													cd.categories_description,
													c.categories_id,
													cd.categories_name,
													cd.categories_heading_title,
													c.categories_image,
													cd.categories_pic_alt,
													c.parent_id
												FROM
													".TABLE_CATEGORIES." c,
													".TABLE_CATEGORIES_DESCRIPTION." cd
												WHERE
													c.categories_status = '1'
												AND
													c.parent_id = '".$category_links[$i]."'
												AND
													c.categories_id = cd.categories_id
													".$group_check_c."
												AND
													cd.language_id = '".(int)$_SESSION['languages_id']."'
												ORDER BY
													sort_order, cd.categories_name", true);

				if($categories->_numOfRows)
	    			break;
			}

		} else {
			$categories = $db->db_query("SELECT
											cd.categories_description,
											c.categories_id,
											cd.categories_name,
											cd.categories_heading_title,
											c.categories_image,
											cd.categories_pic_alt,
											c.parent_id
										FROM
											".TABLE_CATEGORIES." c, ".TABLE_CATEGORIES_DESCRIPTION." cd
										WHERE
											c.categories_status = '1'
										AND
											c.parent_id = '".$current_category_id."'
										AND
											c.categories_id = cd.categories_id
											".$group_check_c."
										AND
											cd.language_id = '".(int)$_SESSION['languages_id']."'
										ORDER BY
											sort_order, cd.categories_name", true);
		}

		$rows = 0;
		while(!$categories->EOF) {
			$rows ++;

			$cPath_new = category_link($categories->fields['categories_id']);

			$image = '';
			if ($categories->fields['categories_image'] != '')
				$image = DIR_WS_IMAGES.'categories/'.$categories->fields['categories_image'];

			$categories_content[] = array ('CATEGORIES_NAME' => $categories->fields['categories_name'],
	  										'CATEGORIES_HEADING_TITLE' => $categories->fields['categories_heading_title'],
	  										'CATEGORIES_IMAGE' => $image,
	  										'CATEGORIES_IMAGE_ALT' => ($categories->fields['categories_pic_alt'] !='' ? $categories->fields['categories_pic_alt'] : $categories->fields['categories_name']),
	  										'CATEGORIES_IMAGE_DIMENSION' => get_img_size($image),
	  										'CATEGORIES_LINK' => href_link(FILENAME_DEFAULT, $cPath_new),
	  										'CATEGORIES_DESCRIPTION' => str_replace('"', '\'', $categories->fields['categories_description']));
	  		$categories->MoveNext();
		}
		$new_products_category_id = $current_category_id;
		include(DIR_WS_MODULES.FILENAME_NEW_PRODUCTS);
		if(PRODUCT_LISTING_SHOW_PRODUCTS == 'true')
			include(DIR_WS_MODULES.'product_categorie_listing.php');

		$image = '';
		if($category->fields['categories_image'] != '')
			$image = DIR_WS_IMAGES.'categories/'.$category->fields['categories_image'];

		$default_smarty->assign('CATEGORIES_NAME', $category->fields['categories_name']);
		$default_smarty->assign('CATEGORIES_HEADING_TITLE', $category->fields['categories_heading_title']);

		$default_smarty->assign('CATEGORIES_IMAGE', $image);
		$default_smarty->assign('CATEGORIES_DESCRIPTION', $category->fields['categories_description']);
		$default_smarty->assign('CATEGORIES_IMAGE_ALT', $category->fields['categories_pic_alt'] !='' ? $category->fields['categories_pic_alt'] : $category->fields['categories_name']);
		$default_smarty->assign('CATEGORIES_IMAGE_DIMENSION', get_img_size($image));

		$default_smarty->assign('language', $_SESSION['language']);
		$default_smarty->assign('CATEGORIES_LIST', $module_categories);
		$default_smarty->assign('module_content', $categories_content);
	
		include(DIR_WS_MODULES.'slider.php');
		
		if($category->fields['categories_template'] == '' || $category->fields['categories_template'] == 'default') {
			$files = array ();
			if($dir = opendir(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/categorie_listing/')) {
				while(($file = readdir($dir)) !== false) {
					if(is_file(DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/categorie_listing/'.$file) && (substr($file, -5) == '.html') && ($file != 'index.html') && (substr($file, 0, 1) !='.'))
						$files[] = $file;
				}
				closedir($dir);
			}
			sort($files);
			$category->fields['categories_template'] = $files[0];
		}

		$default_smarty->caching = false;
		$main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/categorie_listing/'.$category->fields['categories_template']);
		$smarty->assign('main_content', $main_content);

	} elseif ($category_depth == 'products' || ($_GET['brand'] && $_GET['brand'] !='')) {

		if(isset($_GET['brand'])) {
	
	 		$sorting_sql = $db->db_query("SELECT products_sorting, products_sorting2 FROM ".TABLE_CATEGORIES." WHERE categories_id='".$new_products_category_id."'", true);
	
			if($sorting_sql->fields['products_sorting'] == '')
				$sorting_sql->fields['products_sorting'] = 'pd.products_name';
	
		  	if(isset($_GET['filter_id']) && not_null($_GET['filter_id'])) {
				switch ($_GET['multisort']){
				    case 'specialprice':
				      	$sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC ';
						$field = ' INNER JOIN '.TABLE_SPECIALS.' AS s ON (p.products_id = s.products_id) ';
				      	break;
				    case 'new_asc':
				      	$sorting = ' ORDER BY p.products_date_added ASC';
				     	break;
				    case 'new_desc':
				      	$sorting = ' ORDER BY p.products_date_added DESC';
				      	break;
				    case 'name_asc':
				      	$sorting = ' ORDER BY pd.products_name ASC';
				      	break;
				    case 'name_desc':
				      	$sorting = ' ORDER BY pd.products_name DESC';
				      	break;
				    case 'price_asc':
				      	$sorting = ' ORDER BY p.products_price ASC';
				      	break;
				    case 'price_desc':
				      	$sorting = ' ORDER BY p.products_price DESC';
				      	break;
				    case 'brand_asc':
				      	$sorting = ' ORDER BY b.brand_name ASC';
				      	break;
				    case 'brand_desc':
				      	$sorting = ' ORDER BY b.brand_name DESC';
				      	break;
				    default:
				      	$sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
			 	}
	
		    // Nur eine spezielle Kategorie anzeigen
		    $listing_sql = "SELECT DISTINCT
								p.products_fsk18,
								p.products_shippingtime,
								p.products_model,
								pd.products_name,
								p.products_ean,
								p.products_isbn,
								p.products_list_overlay,
								p.products_price,
								p.products_tax_class_id,
								b.brand_name,
								p.products_quantity,
								p.products_image,
								p.products_weight,
								pd.products_short_description,
								pd.products_description,
								pd.products_img_alt,
								p.products_id,
								p.brand_id,
								p.products_price,
								p.products_vpe,
								p.products_vpe_status,
								p.products_vpe_value,
								p.products_discount_allowed,
								p.products_tax_class_id
							FROM
								".TABLE_PRODUCTS_DESCRIPTION." pd,
								".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
								".TABLE_PRODUCTS." p
									INNER JOIN ".TABLE_BRAND." b
									".$field."
							WHERE
								p.products_status = '1'
							AND
								p.brand_id = b.brand_id
							AND
								b.brand_id = '".(int)$_GET['brand']."'
								".$filter_products."
							AND
								p.products_id = p2c.products_id
							AND
								pd.products_id = p2c.products_id
								".$group_check_p."
								".$fsk_lock."
							AND
								pd.language_id = '".(int)$_SESSION['languages_id']."'
							AND
								p2c.categories_id = '".(int)$_GET['filter_id']."'".$sorting;
		  } else {
	
				switch ($_GET['multisort']) {
				    case 'specialprice':
				      	$sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC ';
						$field = ' INNER JOIN '.TABLE_SPECIALS.' AS s ON (p.products_id = s.products_id) ';
				      	break;
				    case 'new_asc':
				      	$sorting = ' ORDER BY p.products_date_added ASC';
				     	break;
				    case 'new_desc':
				      	$sorting = ' ORDER BY p.products_date_added DESC';
				      	break;
				    case 'name_asc':
				      	$sorting = ' ORDER BY pd.products_name ASC';
				      	break;
				    case 'name_desc':
				      	$sorting = ' ORDER BY pd.products_name DESC';
				      	break;
				    case 'price_asc':
				      	$sorting = ' ORDER BY p.products_price ASC';
				      	break;
				    case 'price_desc':
				      	$sorting = ' ORDER BY p.products_price DESC';
				      	break;
				    case 'brand_asc':
				      	$sorting = ' ORDER BY b.brand_name ASC';
				      	break;
				    case 'brand_desc':
				      	$sorting = ' ORDER BY b.brand_name DESC';
				      	break;
				    default:
				    	$sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
				}
				$listing_sql = "SELECT
									p.products_fsk18,
									p.products_shippingtime,
									p.products_model,
									p.products_ean,
									p.products_isbn,
									pd.products_name,
									p.products_id,
									p.products_price,
									p.products_list_overlay,
									b.brand_name,
									p.products_quantity,
									p.products_image,
									pd.products_img_alt,
									p.products_weight,
									pd.products_short_description,
									pd.products_description,
									p.brand_id,
									p.products_vpe,
									p.products_vpe_status,
									p.products_vpe_value,
									p.products_discount_allowed,
									p.products_tax_class_id
								FROM
									".TABLE_PRODUCTS_DESCRIPTION." pd,
									".TABLE_PRODUCTS." p
									INNER JOIN ".TABLE_BRAND." b
									".$field."
								WHERE
									p.products_status = '1'
								AND
									pd.products_id = p.products_id
									".$filter_products."
								AND
									pd.language_id = '".(int)$_SESSION['languages_id']."'
								AND
									p.brand_id = b.brand_id
								AND
									b.brand_id = '".(int)$_GET['brand']."' ".
									$group_check_p.$fsk_lock.$sorting;
				}
	
		  } else {
	
		 	if (isset($_GET['filter_id']) && not_null($_GET['filter_id'])) {
			    $sorting_sql = $db->db_query("SELECT products_sorting, products_sorting2 FROM ".TABLE_CATEGORIES." WHERE categories_id='".$current_category_id."'", true);
	
			    if($sorting_sql->fields['products_sorting']=='')
				    $sorting_sql->fields['products_sorting'] = 'pd.products_name';
			    $sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
	
			    switch ($_GET['multisort']) {
				    case 'specialprice':
				      	$sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC ';
						$field = ' INNER JOIN '.TABLE_SPECIALS.' AS s ON (p.products_id = s.products_id) ';
				      	break;
				    case 'new_asc':
				      	$sorting = ' ORDER BY p.products_date_added ASC';
				      	break;
				    case 'new_desc':
				      	$sorting = ' ORDER BY p.products_date_added DESC';
				      	break;
				    case 'name_asc':
				      	$sorting = ' ORDER BY pd.products_name ASC';
				      	break;
				    case 'name_desc':
				      	$sorting = ' ORDER BY pd.products_name DESC';
				      	break;
				    case 'price_asc':
				      	$sorting = ' ORDER BY p.products_price ASC';
				      	break;
				    case 'price_desc':
				      	$sorting = ' ORDER BY p.products_price DESC';
				      	break;
				    case 'brand_asc':
				      	$sorting = ' ORDER BY b.brand_name ASC';
				      	break;
				    case 'brand_desc':
				      	$sorting = ' ORDER BY b.brand_name DESC';
				      	break;
				    default:
				    	$sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
				    	break;
				}

			    $listing_sql = "SELECT
			    					p.products_fsk18,
									p.products_shippingtime,
									p.products_model,
									p.products_ean,
									p.products_isbn,
									pd.products_name,
									p.products_id,
									b.brand_name,
									p.products_quantity,
									p.products_image,
									p.products_weight,
									p.products_list_overlay,
									pd.products_short_description,
									pd.products_description,
									pd.products_img_alt,
									p.brand_id,
									p.products_price,
									p.products_vpe,
									p.products_vpe_status,
									p.products_vpe_value,
									p.products_discount_allowed,
									p.products_tax_class_id
								FROM
									".TABLE_PRODUCTS_DESCRIPTION." pd,
									".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
									".TABLE_PRODUCTS." p
									INNER JOIN ".TABLE_BRAND." b
									".$field."
								WHERE
									p.products_status = '1'
								AND
									p.brand_id = b.brand_id
								AND
									b.brand_id = '".(int)$_GET['filter_id']."'
								AND
									p.products_id = p2c.products_id
									".$filter_products."
								AND
									pd.products_id = p2c.products_id
								AND
									pd.language_id = '".(int)$_SESSION['languages_id']."'
								AND
									p2c.categories_id = '".$current_category_id."'"
									.$group_check_p.$fsk_lock.$sorting;
			
		} else {
	
		    $sorting_sql = $db->db_query("SELECT products_sorting, products_sorting2 FROM ".TABLE_CATEGORIES." WHERE categories_id = '".$current_category_id."'", true);
		    if(!$sorting_sql->fields['products_sorting'])
		    	$sorting_sql->fields['products_sorting'] = 'pd.products_name';
		    $sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
	
		    switch ($_GET['multisort']) {
			    case 'specialprice':
			      	$sorting = ' ORDER BY s.specials_price_'.$_SESSION['customers_status']['customers_status_id'].' DESC ';
					$field = ' INNER JOIN '.TABLE_SPECIALS.' AS s ON (p.products_id = s.products_id) ';
			      	break;
			    case 'new_asc':
			      	$sorting = ' ORDER BY p.products_date_added ASC';
			      	break;
			    case 'new_desc':
			      	$sorting = ' ORDER BY p.products_date_added DESC';
			      	break;
			    case 'name_asc':
			      	$sorting = ' ORDER BY pd.products_name ASC';
			      	break;
			    case 'name_desc':
			      	$sorting = ' ORDER BY pd.products_name DESC';
			      	break;
			    case 'price_asc':
			      	$sorting = ' ORDER BY p.products_price ASC';
			      	break;
			    case 'price_desc':
			      	$sorting = ' ORDER BY p.products_price DESC';
			      	break;
			    default:
			    	$sorting = ' ORDER BY '.$sorting_sql->fields['products_sorting'].' '.$sorting_sql->fields['products_sorting2'].' ';
			}
			$listing_sql = "SELECT p.products_fsk18,
								p.products_shippingtime,
								p.products_model,
								p.products_ean,
								p.products_isbn,
								p.brand_id,
								pd.products_name,
								p.products_quantity,
								p.products_image,
								p.products_weight,
								p.products_list_overlay,
								pd.products_short_description,
								pd.products_description,
								pd.products_img_alt,
								p.products_id,
								p.products_price,
								p.products_vpe,
								p.products_vpe_status,
								p.products_vpe_value,
								p.products_discount_allowed,
								p.products_tax_class_id
	             			FROM
	             				".TABLE_PRODUCTS." p
							INNER JOIN
								".TABLE_PRODUCTS_TO_CATEGORIES." p2c ON (p2c.categories_id = '".$current_category_id."')
							INNER JOIN
								".TABLE_PRODUCTS_DESCRIPTION." pd ON (pd.products_id = p2c.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')
								$field
							WHERE
								p.products_status = '1'
								".$filter_products."
	                        AND
								p.products_id = p2c.products_id
							".$group_check_p.$fsk_lock.$sorting;
							
				$total_count = $db->db_query("SELECT
													p.products_id
												FROM
													".TABLE_PRODUCTS_TO_CATEGORIES." p2c,
													".TABLE_PRODUCTS." p
													".$field."
												WHERE
													p.products_status = '1'
												AND
													p.brand_id = '".(int)$_GET['filter_id']."'
												AND
													p.products_id = p2c.products_id
													".$filter_products."
												AND
													p2c.categories_id = '".$current_category_id."'"
													.$group_check_p.$fsk_lock, true);
					
				$total_ids = $total_count->_numOfRows;
			}
			
		}
		
		include (DIR_WS_MODULES.FILENAME_PRODUCT_LISTING);

	} else { // Startseite
		if (GROUP_CHECK == 'true')
			$group_check = " AND group_ids LIKE '%c_".$_SESSION['customers_status']['customers_status_id']."_group%'";

		$shop_content = $db->db_query("SELECT
											content_title,
											content_heading,
											content_text,
											content_file
										FROM
											".TABLE_CONTENT_MANAGER."
										WHERE
											content_group = '5'
											".$group_check."
										AND
											languages_id='".$_SESSION['languages_id']."'", true);

		$default_smarty -> assign('title', utf8_decode(($shop_content->fields['content_heading'])));
		include (DIR_WS_INCLUDES.FILENAME_CENTER_MODULES);

		if ($shop_content->fields['content_file'] != '') {
			ob_start();
			if (strpos($shop_content->fields['content_file'], '.txt'))
				echo '<pre>';

			include (DIR_FS_CATALOG.'media/content/'.$shop_content->fields['content_file']);

			if (strpos($shop_content->fields['content_file'], '.txt'))
				echo '</pre>';
			
			$shop_content->fields['content_text'] = ob_get_contents();
			ob_end_clean();
		}
		$default_smarty->assign('text', str_replace('{$greeting}', customer_greeting(), $shop_content->fields['content_text']));
		$default_smarty->assign('language', $_SESSION['language']);
		
		include(DIR_WS_MODULES.'slider.php');
		
		if(!CacheCheck()) {
	  		$default_smarty->caching = false;
	  		$main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/main_content.html');
	  	} else {
	  		$default_smarty->caching = true;
	  		$default_smarty->cache_lifetime = CACHE_LIFETIME;
	  		$default_smarty->cache_modified_check = CACHE_CHECK;
	  		$cache_id = $_SESSION['language'].$_SESSION['currency'].$_SESSION['customer_id'];
	  		$main_content = $default_smarty->fetch(CURRENT_TEMPLATE.'/module/main_content.html', $cache_id);
	  	}
	  	$smarty->assign('main_content', $main_content);
	}
}